package com.internet.viemall;

import java.util.HashSet;
import java.util.Set;

import com.internet.viemall.cache.CacheManager;
import com.internet.viemall.cache.VieMallCacheManager;

public class UserServiceImpl implements UserService {

	private VieMallCacheManager vieMallCacheManager;
	
	
	Set<User> users = new HashSet<User>();
	
	@Override
	public User save(User user) {
		users.add(user);
		vieMallCacheManager.set("user", user.getId(), user);
		return user;
	}

	@Override
	public User update(User user) {
		users.remove(user);
		users.add(user);
		vieMallCacheManager.set("user", user.getId(), user);
		return user;
	}

	@Override
	public User delete(User user) {
		users.remove(user);
		vieMallCacheManager.evict("user", user.getId());
		return user;
	}

	@Override
	public void deleteAll() {
		users.clear();
		vieMallCacheManager.clear("user");
	}

	@Override
	public User findById(Long id) {
		System.out.println("cache miss, invoke find by id, id:" + id);
		return (User) vieMallCacheManager.get("user", id);
	}

	public void setVieMallCacheManager(VieMallCacheManager vieMallCacheManager) {
		this.vieMallCacheManager = vieMallCacheManager;
	}
	
	

}
